# -*- coding: gb2312 -*-
import re
import datetime
import string
import os

file_input_dir = 'logs'
file_output_dir = 'result'
rem_date=re.compile(r'^(\w{3}) (\w{3}) ( |\d)\d')
rem_abnormal = (re.compile('^ERROR:'), re.compile('^ORA-'), re.compile('^WARNING:'), re.compile('^Could not') )
def get_date():
    date_start = datetime.date.today() - datetime.timedelta(days=7)
    delta_start_days = 7
    date_end = datetime.date.today()
    while True:
        prompt = raw_input('Please input start date in form of days from today.[7]')
        if prompt == '':
            break
        delta_start_days = string.atoi(prompt)
        if delta_start_days > 1 and delta_start_days < 366:
            date_start = date_start - datetime.timedelta(days=delta_start_days - 7)
            break
        print 'Invalid prompt! Please input days between 1 and 366'
    while True:
        prompt = raw_input('Please input ending date(in form of day# from today).[Default is today]')
        if prompt == '':
            break
        delta_days = string.atoi(prompt)
        if delta_days > 1 and delta_days < 366 and delta_days < delta_start_days:
            date_end = date_end - datetime.timedelta(days=delta_days)
            break
        print 'Invalid prompt! Please input days bebetween 1 and 366, and less than', delta_start_days
    return (date_start, date_end)

def analyze(s, e, fd_rd, fd_wt, fd_wt_full):
    str_date_cur = ''
    str_date_end =  (e - datetime.timedelta(days= -1)).strftime("%a %b %d")
    date_cur = s
    date_delta = (e - s).days

    #firstly we will find out the start date, but the information after start date cannot be found.
    line = fd_rd.readline()
    tag_started = False
    for i in range(date_delta):
        date_cur = s - datetime.timedelta(days= -i)
        str_date_cur = date_cur.strftime("%a %b %d") #The year(%Y) can be implicit because of the week day.
        while True:
            if line == '':
                break
            if  len(line) == 25:                                                     #for the performance concerned, we use the substring to match datetime lines -- in vim, /^.\{24\} always match the datetime lines!
                if line[0:10] == str_date_cur:                           #log always contains data over one year, and we always need one month or one week. instead of re.match(str_date_cur, line):
                    tag_started = True
                    break
            line = fd_rd.readline()   
        if tag_started:
            break;
        fd_rd.seek(0, 0)
        line = fd_rd.readline()

    #the information after start date cannot be found.
    if tag_started == False:
        print 'Nothing found.'
        return 0

    cur_info = []
    tag_save =False
    while True:
        cur_info.append(line)
        line = fd_rd.readline()
        if line == '':
            break
        #if match the next date line, the previous one is written to output file.
        if rem_date.match(line):
            if tag_save:
                print 'matched date', cur_info
                for tmp_info in cur_info: 
                    tmp_info = tmp_info.replace(',',' ')
                    fd_wt.write(tmp_info.strip() + ', ')
                fd_wt.write('\n')
                tag_save = False
            for tmp_info in cur_info:
                fd_wt_full.write(tmp_info)
            cur_info = []
            continue
        #match the abnormal list of  regular expressions, set tag_save to true if match one of them
        for rem in rem_abnormal:
            if rem.match(line):
                tag_save = True
                break
        #if match the end date, analysis is over
        if line[0:10] == str_date_end:
            break

if __name__ == '__main__': 
    #get start and end date
    (s,e) = get_date()
    if not os.path.exists(file_input_dir):
        exit(0)
    #create the output directory
    createDir = lambda dirname: not os.path.exists(dirname) and os.mkdir(dirname)
    createDir(file_output_dir)
    #get the list of log files in file_input_dir directory
    l = os.listdir(file_input_dir)
    for str_file in l:
        fd_rd = open(file_input_dir + '/' + str_file, 'r')
        fd_wt = open(file_output_dir + '/' + str_file.split('.')[0] + '_result.csv', 'w')
        fd_wt_full = open(file_output_dir + '/' + (str_file.split('.'))[0] + '_result.' + (str_file.split('.'))[1], 'w')
        analyze(s, e, fd_rd, fd_wt, fd_wt_full)
        fd_rd.close()
        fd_wt.close()
    print '----------Completed!----------'